// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'

import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import './assets/iconfont/iconfont.css'
Vue.use(ElementUI);
import App from './App';
import axios from "axios";
import $ from 'jquery';
import selectMk from './plugIn/select/index.js';
//相册模块
import preview from 'vue-photo-preview';
import 'vue-photo-preview/dist/skin.css';
import store from './store';



var option = {
  fullscreenEl: true,
  bgOpacity: 0.5,
}
Vue.use(preview, option)

Vue.use(selectMk);
Vue.config.productionTip = false;
import qs from 'qs';
Vue.prototype.$qs = qs;

Vue.prototype.$axios = axios;
Vue.prototype.mkajax = function (param) {
  var _this = this;
  _this.default = {
    url: 'index.php',
    type: 'get',
    data: {}
  }
  $.extend(_this.default, param);
  var _url = _this.default.url;

  var dtd = $.Deferred();
  var header = {};

  // console.log(_this.$store.state.token);
  // console.log(_this.default);
  var token = localStorage.getItem('token');
  if (token) {
    let path = ''+window.location;
    path=path.split('#')[1];

    token = JSON.parse(token).token;
    // console.log(token)
    // var type = this.default.type.toLocaleUpperCase();
    // var timestamp = Date.parse(new Date());
    // var hashkey = type + "," + this.default.url + ",{" + timestamp + "}";
    // var sha256 = CryptoJS.HmacSHA256(hashkey, token).toString();
    // var word = CryptoJS.enc.Utf8.parse(sha256);
    // var auth = CryptoJS.enc.Base64.stringify(word);
    let timestamp = Math.round(new Date() / 1000);
    let auth = "32131233";
    let Authorization = `token="${token}",timestamp="${timestamp}",auth="${auth}",path="${path}"`;
    header = {
      Authorization: Authorization
    };



  }


  // return
  $.ajax({
    url: _url,
    type: _this.default.type,
    data: _this.default.data,
    timeout: 10000,
    cache: false,
    // headers: (_this.default.type == 'get' || _this.default.type == 'GET') ? '' : header,
    headers: header,
    success: function (res) {
      // console.log(res)
      switch (res.status) {
        case 200:
          dtd.resolve(res, _this.default.data); //成功返回
          break;
        case 400:
          dtd.reject(res);
          _this.$message({ //失败返回
            message: res.message,
            type: 'error',
            duration: 1666
          });
          break;
        case 402:
          dtd.reject(res);
          _this.$message({ //失败返回
            message: res.message,
            type: 'error',
            duration: 1666
          });
          break;
        case 500:
          dtd.reject(res);
          _this.$message({ //失败返回
            message: res.message,
            type: 'error',
            duration: 1666
          });
          localStorage.removeItem('classlist');
          localStorage.removeItem('info');
          localStorage.removeItem('token');
          _this.$router.push({
            path: '/login'
          });
          break;

      }


    },
    error: function (err) {
      //console.log('err1',err)
      dtd.reject(err);
      _this.$message({
        message: '数据错误，请联系管理员',
        type: 'error'
      })
    },
    complete: function (ed) {
      // console.log(1,ed)
      // dtd.reject(ed);//暂时处理
    }
  });
  return dtd.promise();
}
Vue.prototype.checkLogin = function () { //检测登陆状态
  let token = localStorage.getItem('info');
  if (token) {} else {
  
      this.$router.push({
        path: '/login'
      });
    
  }
}
Vue.prototype.countYear = function (e, type) { //计算年级
  var timestamp = (new Date()).valueOf();
  // 获取某个时间格式的时间戳
  var timestamp2 = '';
  if (e.length) {
    timestamp2 = Date.parse(new Date(e));
  } else {
    timestamp2 = Date.parse(new Date(e + '-01-01'));
  }

  var time = Math.ceil((timestamp - timestamp2) / 1000 / 60 / 60 / 24 / 365);
  var str = '';
  if (type == 1) {
    switch (time) {
      case -0:
      case 1:
        str = '小学1年级';
        break;
      case 2:
        str = '小学2年级';
        break;
      case 3:
        str = '小学3年级';
        break;
      case 4:
        str = '小学4年级';
        break;
      case 5:
        str = '小学5年级';
        break;
      case 6:
        str = '小学6年级';
        break;
      default:
        str = '非应届';
        break;
    }
  } else if (type == 2) {
    switch (time) {
      case -0:
      case 1:
        str = '初中1年级';
        break;
      case 2:
        str = '初中2年级';
        break;
      case 3:
        str = '初中3年级';
        break;
      default:
        str = '非应届';
        break;
    }

  } else if (type == 3) {
    switch (time) {
      case -0:
      case 1:
        str = '高中1年级';
        break;
      case 2:
        str = '高中2年级';
        break;
      case 3:
        str = '高中3年级';
        break;
      default:
        str = '非应届';
        break;
    }
  }



  return str
}
Vue.prototype.trim = function (str) {
    return str.replace(/(^\s*)|(\s*$)/g, "");
  },
  // 定义全局点击函数
Vue.prototype.globalClick = function (callback) {
  document.getElementById('main').onclick = function () {
      callback();
  };
};

  /* eslint-disable no-new */
  new Vue({
    el: '#app',
    router,
    store,
    components: {
      App
    },
    template: '<App/>'
  })
